package com.oocl.main;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.mqThread.MqThread;

import POJO.Books;

public class Bootstrap {
	public static void main(String[] args) throws JMSException {
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://10.222.29.173:61616");
		Destination queque = new ActiveMQQueue("matthew");
		Connection con = factory.createConnection();
		con.start();
		final Session ss = con.createSession(true, Session.AUTO_ACKNOWLEDGE);
		
		MessageConsumer comsumer = ss.createConsumer(queque);
		
		final ObjectMapper mapper = new ObjectMapper();
		
		final ExecutorService threadPool = Executors.newFixedThreadPool(12);
		
		final Object o = new Object();   //同步对象
		
		comsumer.setMessageListener(new MessageListener() {
			
			public void onMessage(Message arg0) {
				
				
				try {
					TextMessage text = (TextMessage) arg0;
					String json = text.getText();
					Books book = mapper.readValue(json, Books.class);
					threadPool.execute(new MqThread(book, o));
					ss.commit();
					
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JsonParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JsonMappingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
//				threadPool.execute(new mqThread(arg0, o));
//				new mqThread(arg0, o ,ss);
			}
		});
		
		

	}
}
